package games

import (
	"errors"
	"wzgames/internal/model/gamep"
	"wzgames/pkg/actor/actor"
)

// IGameService 游戏实例服务
type IGameService interface {
	Login(*GameTable)                  // 登录游戏实例
	Logout(*GameTable)                 // 登出游戏实例
	DoMessage(*GameTable, interface{}) // 处理客户端消息
	GetGameId() int32                  // 游戏实例对应的系统游戏ID
	GameInstId() int64                 // 游戏实例ID
	SetGameInst(*gamep.GameInst)
	Update()
	Destroy()
}

type GameFn func(inst *gamep.GameInst) (IGameService, error)

var (
	myGames = make(map[int32]GameFn)
)

func RegisterGame(gameId int32, fn GameFn) {
	if fn == nil {
		panic("[Games]: Game register provider is nil")
	}
	if _, dup := myGames[gameId]; !dup {
		myGames[gameId] = fn
	}
}

func NewGGame(inst *gamep.GameInst) (IGameService, error) {
	if ge, ok := myGames[inst.GameID]; ok {
		return ge(inst)
	}

	return nil, errors.New("unknown game instant id")
}

// IPlayer Player服务
type IPlayer interface {
	ID() uint64
	Pid() *actor.PID
	AgtID() int64
	AgtKey() string
	Name() string
	AUid() string
	UHead() string
	ShowID() string
	ShowName() string
}
